package com.ctsi.security.event.service;

import com.ctsi.commons.event.AbstractObserver;
import com.ctsi.commons.event.BaseEvent;
import com.ctsi.framework.common.startrun.ComponentFactory;
import com.ctsi.organise.common.api.OrganiseFactoryBean;
import com.ctsi.organise.common.model.Person;
import com.ctsi.organise.common.model.UserLogin;
import com.ctsi.security.PermissionRole;
import com.ctsi.security.domain.SRoleUser;
import com.ctsi.security.manager.impl.SecurityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
/**
 * 修改本地角色里的登录人员名称
 * @author wangbf
 */
public class UpdatePersonNameHandler extends AbstractObserver {
	private final  static  Logger logger = LoggerFactory.getLogger(UpdatePersonNameHandler.class);
	private static final long serialVersionUID = 1L;
	public void onEvent(Object[] args) {
		BaseEvent event=(BaseEvent) args[0];
		Person person=(Person) event.getSource();
		logger.info("保存/更新的用户ID为{} ,名称为{} ",person.getPartyId(),person.getFullName());
	 
		SRoleUser sroleUser=new SRoleUser();
		sroleUser.setPartyId(person.getPartyId());
		sroleUser.setLoginName(person.getFullName());
		getSRoleService().updateSoleUserNameByPaytyId(sroleUser);
		logger.info("更新用户的角色缓存，开始" );
		List<UserLogin> listMap = OrganiseFactoryBean.getInstance().getUserLoginProxy().findUserLoginByPartyid(person.getPartyId());
		for(UserLogin map:listMap){
			logger.info("更新用户的角色缓存，当前用户：{}",map.getLoginName());
			SecurityImpl.removeCacheUserRoles(map.getLoginName());
		}
	}
	private PermissionRole getSRoleService(){
		PermissionRole  sRoleService=(PermissionRole) ComponentFactory.getBean("sroleService");
	    return sRoleService;
	}
}
